/*
*author:lzc
*date:2015.05.11
*dependen lib:zepto
*
*opts{
*cntr: ul,
*child: li
*isAuto: true
*isCircle: true
*scrollTime: 300
*autoTime: 2000
*}
*/
function mobileCarouselX(opts){
    var $cntrEl = $(opts.cntrEl)
    , $childEl = $cntrEl.find(opts.child)
    , childNum = $childEl.length
    , childW = $childEl.width()
    , curIndex = 1
    , pos = {}
    , translateX = 0
    , maxNum = childNum-1
    , minNum = 0
    , scrollTime = opts.scrollTime ? opts.scrollTime : 300
    , dir
    , autoTime = opts.autoTime ? opts.autoTime : 2000
    , autoTimeId
    , currentTransX
    function _carousel(){
        function init(){
            var $endEl = $($childEl[maxNum]).clone()
            var $firstEl = $($childEl[0]).clone()
            $cntrEl.prepend($endEl)
            $cntrEl.append($firstEl)
            $cntrEl.width(childW * (childNum + 2))
            currentTransX = -1*childW
            changeCss(0,currentTransX)
            //根据是否循环 启动定时器
            opts.isAuto == undefined || opts.isAuto == true ? timer() : ''
        }
        // 自动轮播定时器
        function timer(){
            autoTimeId = setInterval(function(){
                leftScroll()
                // rightScroll()
            },autoTime)
        }
        // 改变图片位置
        function changeCss(scrollTime,marginL){
            $cntrEl.css({
                '-webkit-backface-visibility': 'hidden'//隐藏被旋转元素的背面
                , '-webkit-transition':'-webkit-transform '+scrollTime+'ms'
                , '-webkit-transform':'translateX('+marginL+'px)'
                , '-moz-transition':'-webkit-transform '+scrollTime+'ms'
                , '-moz-transform':'translateX('+marginL+'px)'
                , 'transition':'-webkit-transform '+scrollTime+'ms'
                , 'transform':'translateX('+marginL+'px)'
            })
        }
        // 滚动位置计算
        function scroll(scrollTime){
            var marginL = curIndex * childW * -1
            currentTransX = marginL
            changeCss(scrollTime,marginL)
            // 边界图片处理
            if(curIndex == childNum + 1) {
                setTimeout(function(){
                    scroll(0)
                    curIndex = 1
                },scrollTime)
            }
            if(curIndex == minNum) {
                setTimeout(function(){
                    scroll(0)
                    curIndex = childNum
                },scrollTime)
            }
        }
        // 左滑动
        function leftScroll(){
            curIndex++
            curIndex = curIndex > childNum + 1 ? childNum + 1 : curIndex
            scroll(scrollTime)
        }
        // 右滑动
        function rightScroll(){
            curIndex--
            curIndex = curIndex < minNum ? minNum : curIndex
            scroll(scrollTime)
        }
        // 滑动方向判断
        function direction(x1,x2){
            return x1 - x2 > 0 ? 'Left' : 'Right'
        }
        // 滑动距离判断
        function isScroll(){
            var dir = direction(pos.startX,pos.currentX)
            var deltaX = Math.abs(pos.currentX - pos.startX)
            if(deltaX > childW / 3){
                dir == 'Left' ? leftScroll() : rightScroll()
            }
            else{
                changeCss(scrollTime,currentTransX)
            }
        }
        // 闭包函数
        return {
            init : init
            , timer : timer
            , changeCss : changeCss
            , isScroll : isScroll
        }
    }

    var carousel = new _carousel()
    carousel.init()
    var moveFlag = false

    $cntrEl.on('touchstart',function(e){
        if(e.touches.length == 1){
        }
        pos.startX = e.touches[0].pageX
        pos.startY = e.touches[0].pageY
    })

    $cntrEl.on('touchmove',function(e){
        // 移动时候，禁用timer
        if(!moveFlag && autoTimeId){
            clearInterval(autoTimeId)
            autoTimeId = undefined
        }
        moveFlag = true
        pos.currentX = e.touches[0].pageX
        pos.currentY = e.touches[0].pageY
        var deltaX = currentTransX + e.touches[0].pageX - pos.startX
        carousel.changeCss(0,deltaX)
        return false
    })

    $cntrEl.on('touchend',function(e){
        // 完成触摸后按需启用定时器
        if(moveFlag){
            carousel.isScroll()
            opts.isAuto == undefined || opts.isAuto == true ? carousel.timer() : ''
            moveFlag = false
        }
    })
}